/**
* Created by wust on 2019/5/16.
*/
<template>
  <el-form ref="formModel" :model="formModel" :rules="rules" label-width="120px" style="width:60%;" @submit.native.prevent>
    <el-form-item label="网关名称" prop="name">
      <el-input v-model="formModel.name"></el-input>
    </el-form-item>
    <el-form-item label="MAC地址" prop="mac">
      <el-input v-model="formModel.mac" disabled></el-input>
    </el-form-item>
    <el-form-item label="选择区域">
      <el-cascader filterable v-model="formModel.areaId" :options="areaCascaderOptions" @change="areaCascaderHandleChange"></el-cascader>
    </el-form-item>
    <el-form-item label="备注">
      <el-input
        v-model="formModel.description"
        type="textarea"
        :rows="6">
      </el-input>
    </el-form-item>
    <el-form-item style="text-align: left;">
      <el-button type="primary" @click="onSubmit('formModel')" :loading="submitting">提交</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import Vue from 'vue'

export default {
  name: 'demo-update',
  props: ['selectedModel'],
  data () {
    return {
      submitting: false,
      formModel: {
        id: '',
        name: '',
        mac: '',
        areaId: null,
        description: ''
      },
      areaCascaderOptions: [],
      areaCascaderValue: []
    }
  },
  created: function () {
    this.formModel = this.selectedModel
    this.getAreaCascader()
  },
  methods: {
    getAreaCascader: function () {
      Vue.$ajax({
        method: 'post',
        url: Vue.$demoServerURL + '/web/v1/AreaController/buildCascader'
      }).then(res => {
        if (res.data.flag !== 'SUCCESS') {
          if (!Vue.$isNullOrIsBlankOrIsUndefined(res.data.message)) {
            this.$message({
              message: res.data.message,
              type: 'warning'
            })
          }
        } else {
          this.areaCascaderOptions = res.data.obj
        }
      })
    },
    areaCascaderHandleChange: function (val) {
      if (val == null || val.length === 0) {
        return
      }
      this.formModel.areaId = val[val.length - 1]
    },
    onSubmit: function (formData) {
      this.$refs[formData].validate((valid) => {
        if (!valid) {
        } else {
          this.submitting = true
          Vue.$ajax({
            method: 'put',
            url: Vue.$demoServerURL + '/web/v1/GatewayController',
            data: this.formModel
          }).then(res => {
            this.submitting = false
            if (res.data.flag !== 'SUCCESS') {
              if (!Vue.$isNullOrIsBlankOrIsUndefined(res.data.message)) {
                this.$message({
                  message: res.data.message,
                  type: 'warning'
                })
              }
            } else {
              this.$message({
                message: res.data.message,
                type: 'success'
              })
            }
          })
        }
      })
    }
  }
}
</script>
